package cn.lyd.org.controller;

import cn.lyd.basic.exception.BusinessException;
import cn.lyd.basic.util.ExcelUtils;
import cn.lyd.org.domain.ShopAuditLog;
import cn.lyd.org.dto.ShopDto;
import cn.lyd.org.service.IShopService;
import cn.lyd.org.domain.Shop;
import cn.lyd.org.query.ShopQuery;
import cn.lyd.basic.util.AjaxResult;
import cn.lyd.basic.util.PageList;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/shop")
public class ShopController {

    @Autowired
    public IShopService shopService;

    /**
     * 接口：添加或修改
     * @param shop  传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.add(shop);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
    * 接口：删除
    * @param id
    * @return AjaxResult 响应给前端
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
        e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return AjaxResult 响应给前端
    */
    @PatchMapping
    public AjaxResult patchDel(@RequestBody Long[] ids){
        try {
                shopService.patchDel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"批量删除失败");
        }
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @GetMapping("/{id}")
    public Shop findById(@PathVariable("id")Long id) {
        return shopService.findById(id);
    }


    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping
    public List<Shop> findAll(){
        return shopService.findAll();
    }


   /**
    * 接口：分页查询或高级查询
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public PageList<Shop> queryPage(@RequestBody ShopQuery query) {
        return shopService.queryPage(query);
    }


    /**
     * 接口：店铺入驻
     * @param shop
     * @return
     */
    @PostMapping("/settlement")
    @ApiOperation(value = "店铺入驻")
    public AjaxResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            return new AjaxResult();
        } catch (BusinessException e) {
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后重试！！！");
        }
    }


    /**
     * 店铺入驻审核-通过
     * @param log
     * @return
     */
    @PostMapping("/audit/pass")
    @ApiOperation(value = "店铺审核通过")
    public AjaxResult pass(@RequestBody ShopAuditLog log){
        try {
            shopService.pass(log);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }


    /**
     * 店铺激活
     * @param shopId
     * @return
     */
    @GetMapping("/active/{shopId}")
    @ApiOperation(value = "店铺激活")
    public String active(@PathVariable("shopId")Long shopId){
        try {
//             int a =1/0;
            shopService.active(shopId);
           return "<h1>您的店铺激活成功！</h1>";
//            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
//            return new AjaxResult(false,"操作失败");
            return "<h1>激活失败！请联系管理员！</h1>";
        }
    }



    /**
     * 店铺入驻审核-不通过
     * @param log
     * @return
     */
    @PostMapping("/audit/reject")
    @ApiOperation(value = "店铺审核驳回")
    public AjaxResult reject(@RequestBody ShopAuditLog log){
        try {
            shopService.reject(log);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }


    /**
     * 通过工具类-导出店铺
     * @param response
     */
        /*public static void exportExcel(
                List<?> list,   //导出的数据--集合
                String title,   //导出的标题
                String sheetName,//sheetName页名称
                Class<?> pojoClass,//导出类型
                String fileName,//文件名
                HttpServletResponse response//响应对象
                ) */
    @GetMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response){
        //获取list
        List<Shop> shops = shopService.findAll();
        //使用工具类导出方法
        ExcelUtils.exportExcel(shops,null,"店铺信息",Shop.class,"shop.xlsx",response);
    }

    /**
     * 通过工具类-导入店铺
     * @param file
     */
    @PostMapping("/importExcel")
    public void importExcel(@RequestPart("file") MultipartFile file){

        /*
        public static <T> List<T> importExcel(
        MultipartFile file, //文件对象
        Integer titleRows,//忽略标题行数
        Integer headerRows,//忽略表头行数
        Class<T> pojoClass)//生成的对象类型
        */
        //通过工具类-导入店铺
        List<Shop> shops = ExcelUtils.importExcel(file, 0, 1, Shop.class);
        //后台查看导入的信息
        shops.forEach(System.out::println);

            //将导入的信息添加到表中
            /*for (Shop shop : shops) {
                shopService.add(shop);
            }*/
            //将导入的信息修改到表中
            /*for (Shop shop : shops) {
                shopService.update(shop);
            }*/

            //法二：批量插入

    }


    /**
     * 报表
     * @return
     */
    @GetMapping("/echarts")
    public List<ShopDto> echartsData(){
       List<ShopDto> shopDtos = shopService.getCountByState();
        return  shopDtos;

    }

}
